﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.ModelBinding;
using System.Web.Http.ValueProviders;
using Xinchen.Utils;

namespace Xinchen.SOA.Server.ServiceHosting
{
    public class ActionValueBinder : DefaultActionValueBinder
    {
        protected override HttpParameterBinding GetParameterBinding(HttpParameterDescriptor parameter)
        {
            ParameterBindingAttribute parameterBinderAttribute = parameter.ParameterBinderAttribute;
            if (parameterBinderAttribute == null)
            {
                ParameterBindingRulesCollection parameterBindingRules = parameter.Configuration.ParameterBindingRules;
                if (parameterBindingRules != null)
                {
                    HttpParameterBinding binding = parameterBindingRules.LookupBinding(parameter);
                    if (binding != null)
                    {
                        return binding;
                    }
                }
                if (TypeHelper.IsValueType(parameter.ParameterType))
                {
                    return parameter.BindWithAttribute(new ValueProviderAttribute(typeof(MyValueProviderFactory)));
                }
                parameterBinderAttribute = new FromBodyAttribute();
            }
            return parameterBinderAttribute.GetBinding(parameter);
        }
    }
}
